<template>
    <div
        v-if="isExternalBol"
        :style="styleExternalIcon"
        class="svg-external-icon svg-icon"
        v-bind="$attrs"
    />
    <svg v-else :class="svgClass" aria-hidden="true" v-bind="$attrs">
        <use :xlink:href="iconName" />
    </svg>
</template>

<script lang="ts" setup>
// doc: https://panjiachen.github.io/vue-element-admin-site/feature/component/svg-icon.html#usage
import { isExternal } from '@/utils/validate';
import { computed } from 'vue';
const props = defineProps({
    iconClass: {
        type: String,
        required: true
    },
    className: {
        type: String,
        default: ''
    }
});
const isExternalBol = computed(() => {
    return isExternal(props.iconClass);
});
const iconName = computed(() => {
    return `#icon-${props.iconClass}`;
});
const svgClass = computed(() => {
    if (props.className) {
        return 'svg-icon ' + props.className;
    } else {
        return 'svg-icon';
    }
});
const styleExternalIcon = computed(() => {
    return {
        mask: `url(${props.iconClass}) no-repeat 50% 50%`,
        '-webkit-mask': `url(${props.iconClass}) no-repeat 50% 50%`
    };
});
</script>

<style scoped>
.svg-icon {
    overflow: hidden;
    width: 1em;
    height: 1em;
    vertical-align: -0.15em;
    fill: currentcolor;
}
.svg-external-icon {
    display: inline-block;
    background-color: currentcolor;
    mask-size: cover !important;
}
</style>
